const Permiss = require('../models/Permission')
const dayjs = require('dayjs')
class Permiss_C {
    static async findByPk(req, res) {
        const { id } = req.query
        try {
            const results = await Permiss.findByPk(id)
            res.json({
                code: 0,
                msg: "服务器请求成功",
                results: results
            })
        } catch (error) {
            res.json({ code: -1, msg: "服务器请求失败", error })
        }
    }
    static async create(req, res) {
        const { name, permission_code, description } = req.body
        const created_at = dayjs().format('YYYY-MM-DD HH:mm:ss');
        try {
            const results = await Permiss.create(name, permission_code, description, created_at)
            res.json({
                code: 0,
                msg: "服务器请求成功",
                results: results
            })
        } catch (error) {
            res.json({ code: -1, msg: "服务器请求失败", error })
        }
    }
    // 权限删除--真删除
    static del_permis_t = (req, res) => {
        let { id } = req.query
        let p = Permiss.del_permis_t(id)
        p.then((results) => {
            console.log(results)
            res.json({ code: 0, msg: "权限删除成功！", results })
        }).catch((error) => {
            console.log(error)
            res.json({ code: -1, msg: "权限删除失败！", error })
        })
    }
    // 权限修改--设置数据
    static permis_set = (req, res) => {
        let { name, description, permission_code, id } = req.body
        const updated_at = dayjs().format('YYYY-MM-DD HH:mm:ss');
        let p = Permiss.permis_set(name, description, updated_at, permission_code, id)
        p.then((results) => {
            console.log(results)
            res.json({ code: 0, msg: "权限修改--设置数据成功！", results })
        }).catch((error) => {
            console.log(error)
            res.json({ code: -1, msg: "权限修改--设置数据失败！", error })
        })
    }
    // 列表搜索--分页
    static permis_list_c = (req, res) => {
        let { name, sign, page, page_size } = req.query
        let p = Permiss.permis_list(name, sign, Number(page), Number(page_size))
        p.then((results) => {
            console.log(results)
            res.json({ code: 0, msg: "列表搜索加载成功！", results })
        }).catch((error) => {
            console.log(error)
            res.json({ code: -1, msg: "列表搜索加载失败！", error })
        })
    }
    // 全部权限接口
    static permis_all = (req, res) => {
        let p = Permiss.permis_all()
        p.then((results) => {
            console.log(results)
            res.json({ code: 0, msg: "全部权限加载成功！", results })
        }).catch((error) => {
            console.log(error)
            res.json({ code: -1, msg: "全部权限加载失败！", error })
        })
    }
    // 权限的对应角色
    static roles_get = (req, res) => {
        const { permiss_id } = req.query
        let p = Permiss.roles_get(Number(permiss_id))
        p.then((results) => {
            console.log(results)
            res.json({ code: 0, msg: "权限的对应角色加载成功！", results })
        }).catch((error) => {
            console.log(error)
            res.json({ code: -1, msg: "权限的对应角色加载失败！", error })
        })
    }
    // 将权限分配给角色
    static roles_set = (req,res) => {
        const { permiss_id,role_ids } = req.query
        console.log("role_ids:::::",role_ids,permiss_id)
        if(!permiss_id && !role_ids){
            res.json({ code: -1, msg: "缺少必要的参数" })
        }
        let p = Permiss.roles_set(Number(permiss_id),role_ids)
        console.log("role_ids,",role_ids)
        p.then((results) => {
            console.log(results)
            res.json({ code: 0, msg: "权限的对应角色设置成功！", results })
        }).catch((error) => {
            console.log(error)
            res.json({ code: -1, msg: "权限的对应角色设置失败！", error })
        })
    }
}

module.exports = Permiss_C 